#import "DDViewController.h"
@interface DDViewController ()<JTWRequestDelegate>
@end
@implementation DDViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CELL_LINE_COLOR;
    self.ddview.backgroundColor = CELL_LINE_COLOR;
}
- (DDView *)ddview
{
    if (!_ddview)
    {
        _ddview = [[DDView alloc] init];
        _ddview.bar.backBtn.hidden = ([NSStringFromClass([self class]) isEqualToString:@"DDLoginViewController"])?YES:NO;
        _ddview.bar.cancelBtn.hidden = NO;
        NSLog(@" max = %f min = %f",SCREEN_MAX_LENGTH, SCREEN_MIN_LENGTH);
        [self.view addSubview:_ddview];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _ddview.c_edges.equal(UIEdgeInsetsZero);
        else
        {
            UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
            if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
                _ddview.c_edges.equal(UIEdgeInsetsMake((SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, GAP, -(SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, -GAP));
            else
                _ddview.c_edges.equal(UIEdgeInsetsMake(GAP, (SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, - GAP, - (SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0));
        }
    }
    return _ddview;
}
- (DDRequest *)request
{
    if (!_request)
    {
        _request = [[DDRequest alloc]init];
        _request.delegate = self;
    }
    return _request;
}
- (void)setStatusBarBackgroundColor:(UIColor *)color
{
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
- (void)yjd_pop:(void (^)(void))block
{
    UIWindow *window = [UIWindow getkeyWindow];
    
    self.modalTransitionStyle =  UIModalTransitionStyleCoverVertical;
    self.modalPresentationStyle = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?UIModalPresentationFormSheet:UIModalPresentationOverCurrentContext;
    self.view.backgroundColor = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?[UIColor whiteColor]:[UIColor clearColor];
    if (window.rootViewController.presentedViewController)
    {
        DeLog(@"当前窗口没有退出 %@",window.rootViewController.presentedViewController);
        [window.rootViewController presentViewController:self animated:YES completion:^{
            if (block)
            {
                block();
            }
        }];
    }
    else
    {
        [window.rootViewController presentViewController:self animated:YES completion:^{
            if (block)
            {
                block();
            }
        }];
    }
}
- (void)yjd_screenRotate:(UIInterfaceOrientation)orientation
{
    switch (orientation) {
        case UIInterfaceOrientationUnknown:
            DeLog(@"未知方向");
            break;
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
                _ddview.c_edges.equal(UIEdgeInsetsMake((SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, GAP, -(SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, -GAP));
            
            DeLog(@"界面直立/倒立");
            break;
            
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
                _ddview.c_edges.equal(UIEdgeInsetsMake(GAP, (SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0, - GAP, - (SCREEN_MAX_LENGTH - SCREEN_MIN_LENGTH) / 2.0));
            
            DeLog(@"界面朝左/右");
            break;
            
        default:
            break;
    }
}
#pragma mark - Notification method
- (void)yjd_keyboardWillShow:(NSNotification *)notifi
{
    NSDictionary *userInfo = [notifi userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardHeight = [aValue CGRectValue].size.height;
    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat maxY = 0;
    UIView *view = [self firstResponder:self.view];
    if ([view isKindOfClass:NSClassFromString(@"WKContentView")])
    {
        return;
    }
    maxY =  CGRectGetMaxY([view convertRect:view.bounds toView:[[[UIApplication sharedApplication] delegate] window]]);
    CGFloat map = SCREEN_HEIGHT - maxY - keyboardHeight;
    if (map < 0)
    {
        [UIView animateWithDuration:time animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, map);
        }];
    }
}
- (void)yjd_keyboardWillHide:(NSNotification *)notifi
{
    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:time animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}
- (void)yjd_viewRotate:(NSNotification *)notifi
{
    [self yjd_screenRotate:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (void)yjd_backToPreviousViewController
{
    if ([self isEqual:[UIViewController currentVC]])
    {
        [self dismissViewControllerAnimated:YES completion:^{
            if ([NSStringFromClass([self class]) isEqualToString:@"DDLoginViewController"] || [NSStringFromClass([self class]) isEqualToString:@"DDZFViewController"])
            {
                [CCNotice post:DDPlatformCancelNotification obj:nil info:nil];
            }
        }];
    }
}

- (void)yjd_cancelCurrentViewController
{
    self.view.hidden = YES;
    UIViewController *vc = self.presentingViewController;
    while (vc.presentingViewController)
    {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:NO completion:^{
        [CCNotice post:DDPlatformCancelNotification obj:nil info:nil];
    }];
}
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    viewControllerToPresent.modalTransitionStyle =  UIModalTransitionStyleCoverVertical;
    viewControllerToPresent.modalPresentationStyle = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?UIModalPresentationFormSheet:UIModalPresentationOverCurrentContext;
    viewControllerToPresent.view.backgroundColor = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?[UIColor whiteColor]:[UIColor clearColor];
    [super presentViewController:viewControllerToPresent animated:flag completion:completion];
}
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion
{
    if (self.modalPresentationStyle == UIModalPresentationFormSheet || self.modalPresentationStyle == UIModalPresentationOverCurrentContext)
    {
        if ([self.presentingViewController respondsToSelector:@selector(reload)])
        {
            DDViewController * vc = (DDViewController *)self.presentingViewController;
            [vc reload];
        }
    }
    [super dismissViewControllerAnimated:flag completion:completion];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [CCNotice addObs:self sel:@selector(yjd_keyboardWillShow:) name:UIKeyboardWillShowNotification obj:nil];
    [CCNotice addObs:self sel:@selector(yjd_keyboardWillHide:) name:UIKeyboardWillHideNotification obj:nil];
    [CCNotice addObs:self sel:@selector(yjd_viewRotate:) name:UIApplicationDidChangeStatusBarOrientationNotification obj:nil];
    [CCNotice addObs:self sel:@selector(yjd_backToPreviousViewController) name:CCNoticeNameDDViewBackBtnMethod obj:nil];
    [CCNotice addObs:self sel:@selector(yjd_cancelCurrentViewController) name:CCNoticeNameDDViewCancelBtnMethod obj:nil];
    
    //    self.ddview.bar.title = helpermanger().getPara().name;
    //    self.ddview.hidden = NO;
    self.ddview.bar.title = helpermanger().getPara().name;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [CCHud stopHud];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [CCNotice rmObs:self name:UIKeyboardWillShowNotification obj:nil];
    [CCNotice rmObs:self name:UIKeyboardWillHideNotification obj:nil];
    [CCNotice rmObs:self name:UIApplicationDidChangeStatusBarOrientationNotification obj:nil];
    [CCNotice rmObs:self name:CCNoticeNameDDViewBackBtnMethod obj:nil];
    [CCNotice rmObs:self name:CCNoticeNameDDViewCancelBtnMethod obj:nil];
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
- (void)dealloc
{
}
- (void)reload
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
